package cn.zkcloud.servlet;

import cn.zkcloud.handler.Handler;
import cn.zkcloud.handler.StaticResourcesHandler;
import org.apache.log4j.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author zk
 * @Date 2017/6/17.
 * @Email 2117251154@qq.com
 */
public class DispatcherServlet extends HttpServlet {
    private Logger logger = Logger.getLogger(DispatcherServlet.class);

    private List<Handler> handlerList = new LinkedList<Handler>();

    @Override
    public void init() throws ServletException {
        handlerList.add(new StaticResourcesHandler(getServletContext()));
        logger.debug("init DispatcherServlet...");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpContext httpContext = new HttpContext(req,resp);

        Iterator<Handler> iterator = handlerList.iterator();
        iterator.next().handler(httpContext,iterator);
    }
}
